// 全局LocalStorage存储数据的操作类

const tokenKey = window.globle.tokenKey;
const userKey = "user";
const dictKey = "dicts";
const configKey = "configs";
const menusKey = "menus";
const powersKey = "power";
const loginKey = "login";

/**
 * 浏览器Local Storage操作类
 * 主要用来存储对象
 */
class Storage {
  constructor() {
    this.storage = window.localStorage;
    this.prefix = "lt_"; //存储前缀名
    this.countId = 0; //递归遍历树形结构 加入的唯一id
  }

  /* ---------- storage的基础操作 ---------- */

  /**
   * 新增
   * @param key
   * @param value 对象将被转成json字符串存储
   * @param fn
   */
  insertObject(key, value, fn) {
    try {
      value = JSON.stringify(value);
    } catch (e) {}
    this.storage.setItem(this.prefix + key, value);
    fn && fn();
  }

  set(key, value, fn) {
    this.storage.setItem(this.prefix + key, value);
    fn && fn();
  }

  /**
   * 查询
   * @param key
   * @param fn
   */
  selectObject(key, fn) {
    if (!key) {
      throw new Error("can not found the key");
    }
    if (typeof key === "object") {
      throw new Error(" the key can not be an object");
    }
    var value = this.storage.getItem(this.prefix + key);
    if (value !== null) {
      try {
        value = JSON.parse(value);
      } catch (e) {
        return null;
      }
    }
    return value;
  }

  get(key, fn) {
    if (!key) {
      throw new Error("can not found the key");
    }
    if (typeof key === "object") {
      throw new Error(" the key can not be an object");
    }
    return this.storage.getItem(this.prefix + key);
  }

  /**
   * 删
   * @param key
   */
  delete(key) {
    this.storage.removeItem(this.prefix + key);
  }

  /**
   * 改
   * @param key
   * @param value
   * @param fn
   */
  update(key, value, fn) {
    this.insert(key, value, fn);
  }

  /**
   * 清除所有
   */
  clear() {
    this.storage.clear();
  }

  /* ---------- Token 操作 ---------- */

  /* token相关数据 */
  getToken() {
    let cur = new Date().getTime();
    let tokenObj = this.selectObject(tokenKey);
    if (tokenObj) {
      if (tokenObj.time && cur - window.globle.expire < tokenObj.time) {
        return tokenObj.token;
      }
    }

    return false;
  }
  setToken(token) {
    this.insertObject(tokenKey, {
      token: token,
      time: new Date().getTime()
    });
  }
  refreshToken() { // 刷新token的时间，防止过期
    let tokenObj = this.selectObject(tokenKey);
    if (tokenObj) {
      this.insertObject(tokenKey, {
        token: tokenObj.token,
        time: new Date().getTime()
      });
    }
    return false;
  }
  delToken() {
    this.delete(tokenKey);
  }

  /* ---------- LoginState 操作 ---------- */

  /* 用户登陆状态的相关操作 */
  setLoginState(login) {
    this.insertObject(loginKey, login);
  }
  getLoginState() {
    return this.selectObject(loginKey) || false;
  }

  /* ---------- User 操作 ---------- */

  /* 用户相关 */
  getUser() {
    return this.selectObject(userKey) || "";
  }
  setUser(user) {
    this.insertObject(userKey, user);
  }
  removeUser() {
    this.delete(userKey);
  }

  /* ---------- Dict 操作 ---------- */

  /* 字典相关的数据处理 */
  // 获取字典相关内容，格式：{value:label}
  getDict(pCode) {
    let dicts = this.selectObject(dictKey) || {};
    if (dicts) {
      return dicts.dictItems[pCode];
    }
    return [];
  }
  // 获取字典相关内容，格式：[{value:value,label:label}]
  getSelectDict(pCode) {
    let dicts = this.selectObject(dictKey) || {};
    if (dicts) {
      return dicts.dictItemArr[pCode];
    }
    return [];
  }
  setDict(dicts) {
    this.insertObject(dictKey, dicts);
  }
  removeDict() {
    this.delete(dictKey);
  }

  /* ---------- Config 操作 ---------- */

  /* 配置相关的数据处理 */
  getConfig(key) {
    var configs = this.selectObject(configKey) || {};
    return configs[key];
  }
  setConfigs(configs) {
    this.insertObject(configKey, configs);
  }

  setConfigsKey(key, val) {
    var configs = this.selectObject(configKey) || {};
    configs[key] = val;
    this.insertObject(configKey, configs);
  }

  removeConfigs() {
    this.delete(configKey);
  }

  /* ---------- Menu 操作 ---------- */

  /* 菜单的相关的处理 */
  setMenus(menus) {
    this.insertObject(menusKey, menus);
  }
  getMenus() {
    return this.selectObject(menusKey) || [];
  }
  removeMenus() {
    this.delete(menusKey);
  }

  /* ---------- Power 操作 ---------- */

  /* 权限的相关的处理 */
  setPowers(powers) {
    this.insertObject(powersKey, powers);
  }
  getPowers() {
    return this.selectObject(powersKey) || [];
  }
  removePowers() {
    this.delete(powersKey);
  }


}

export default new Storage();